package kr.makjang.lib;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

public class SlideButton extends SlidingDrawer implements Checkable,
    OnDrawerOpenListener, OnDrawerCloseListener
{
    private int mButtonResource = 0;
    
    private OnCheckChangedListener mOnCheckChangedListener;
    
    /**
     * Constructor
     * @param context
     * @param attrs
     */
    public SlideButton(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        
        setOnDrawerOpenListener(this);
        setOnDrawerCloseListener(this);
    }

    @Override
    public boolean isChecked()
    {
        // TODO Auto-generated method stub
        return !isOpened();
    }
    
    @Override
    public void setChecked(boolean checked)
    {
        if (!isOpened() != checked)
            this.animateClose();
        else
            this.animateOpen();
    }
        
    @Override
    public void toggle()
    {
        if (!isOpened())
            this.animateClose();
        else
            this.animateOpen();
    }
    
    public interface OnCheckChangedListener
    {
        public void onCheckChanged(View view, boolean isChecked);
    }

    @Override
    public void onDrawerOpened()
    {
        if (mOnCheckChangedListener != null)
            mOnCheckChangedListener.onCheckChanged(this, isChecked());
    }

    @Override
    public void onDrawerClosed()
    {
        if (mOnCheckChangedListener != null)
            mOnCheckChangedListener.onCheckChanged(this, isChecked());
    }
    
    public OnCheckChangedListener getOnCheckChangedListener()
    {
        return mOnCheckChangedListener;
    }
    
    public void setOnCheckChangedListener(OnCheckChangedListener onCheckChangedListener)
    {
        this.mOnCheckChangedListener = onCheckChangedListener;
    }
}
